{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "061992a4",
   "metadata": {},
   "source": [
    "### 循环"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fe238bd",
   "metadata": {},
   "source": [
    "python的循环有两种，一种是for循环，以此把list或者tuple中的每个元素迭代出来"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "590f6ad9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Michael\n",
      "Bob\n",
      "Tracy\n"
     ]
    }
   ],
   "source": [
    "names = ['Michael', 'Bob', 'Tracy']\n",
    "for name in names:\n",
    "    print(name)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9795b4d0",
   "metadata": {},
   "source": [
    "python提供一个range()函数，可以生成一个整数序列，再通过list函数可以转换尾list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "0f7e2b7e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in list(range(10)):\n",
    "    print(i)\n",
    "    "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4136b339",
   "metadata": {},
   "source": [
    "第二中循环是while循环，只要条件满足就不断循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e036dc93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2500\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "n = 99\n",
    "while n > 0:\n",
    "    sum = sum + n\n",
    "    n -= 2\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a73a160a",
   "metadata": {},
   "source": [
    "#### break"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "608ca468",
   "metadata": {},
   "source": [
    "在循环中，break可以提前推出循环，与C中break用法一致，不再复述"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d5d53f3",
   "metadata": {},
   "source": [
    "#### continue"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "217dce2a",
   "metadata": {},
   "source": [
    "continue,跳出当前的这次循环，直接开始下一次循环，用法与C一致，不再复述"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc2f49c2",
   "metadata": {},
   "source": [
    "### 小结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12e04980",
   "metadata": {},
   "source": [
    "有时候代码写得有问题，会让程序陷入“死循环”中，这时可以用ctrl+c退出程序，或者强制结束python进程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29fe2902",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
